package com.sykj.cloud.mq.demo.base;

import com.sykj.cloud.mq.demo.Constant;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import static com.sykj.cloud.mq.demo.Constant.*;

/**
 * @author Mr.will
 * @date 2021/3/4 10:39
 * @Desc
 */
public class AsyncProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer mqProducer=new DefaultMQProducer("group1");
        mqProducer.setNamesrvAddr(SERVER_URL);
        mqProducer.start();


        for (int i = 0; i < 10; i++) {

        // 准备消息体
        String body="咦，健子，RockerMQ奥利给！";
        // 发送异步消息
        Message message=new Message(TOPIC_3,TAG_3,body.getBytes());

        mqProducer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功啦，"+sendResult);
            }

            @Override
            public void onException(Throwable e) {
                System.out.println("报错球了"+e);
            }
        });

            Thread.sleep(1000);
        }

        mqProducer.shutdown();
    }

}
